package com.example.exam.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.JwtUtils;
import com.example.commonutils.ResultData;
import com.example.exam.dto.ExamTopicTchDTO;
import com.example.exam.dto.StuSubmitExamDTO;
import com.example.exam.entity.Exam;
import com.example.exam.entity.QueryCondition;
import com.example.exam.service.ExamService;
import com.example.exam.service.MarkExamService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 江晨晖
 * @since 2022-12-15
 */
@RestController
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private ExamService examService;

    @Autowired
    private MarkExamService markExamService;

    @PostMapping("/getTestPaper")
    public ResultData getTestPaperByU_id(@RequestBody QueryCondition queryCondition,HttpServletRequest request){
        return examService.getTestPaper(queryCondition,request);
    }

    //更新试卷
    @PostMapping(value = "/updateTestPaper" )
    public ResultData updateTestPaper(@RequestBody ExamTopicTchDTO examTopicTchDTO){
        return  examService.updateByPrimaryKeySelective(examTopicTchDTO);

    }

    //创建试卷
    @PostMapping(value = "/createTestPaper")
    public ResultData createTestPaper(@RequestBody ExamTopicTchDTO examTopicTchDTO, HttpServletRequest request){
        return examService.insert(examTopicTchDTO,0);
    }

    //通过试卷id获取试卷信息(教师角色)
    @GetMapping("/getTestPaperByTpId/{examId}")
    public ResultData getTestPaperByTp_id(@PathVariable("examId") int examId){
        return examService.selectByPrimaryKey(examId);
    }

    @GetMapping("/getTestPaperStu/{examId}")
    public ResultData getTestPaperStu(@PathVariable("examId") Integer examId, HttpServletRequest request){
        return examService.stuSelectByPrimaryKey(examId,request);
    }

    @DeleteMapping("/deleteTestPaperById/{id}")
    public ResultData deleteTestPaperById(@PathVariable("id") Integer id){
        return examService.deleteTestPaperById(id);
    }

    @PostMapping(value = "/submitTestPaper" )
    public ResultData submitTestPaper(@RequestBody StuSubmitExamDTO stuSubmitExamDTO, HttpServletRequest request){
        System.out.println(stuSubmitExamDTO);
        String token =  request.getHeader("token");
        String u_id = JwtUtils.getMemberIdByJwtToken(request);
        return markExamService.submitTestPaper(stuSubmitExamDTO,u_id);
    }
}
